package shipNuking;

public class Player {
	
	public void placeShip(int x1, int y1, int x2, int y2, Ship z,
			Ship[][] ships) {
		// takes the coordinates for the ship an fills the indices between
		// the two points with references of the same ship object
		if (x1 == x2) {
			if (y1 < y2) {
				for (int i = y1; i <= y2; i++) {
					ships[x1][i] = z;
				}
			} else if (y1 > y2) {
				for (int i = y1; i >= y2; i--) {
					ships[x1][i] = z;
				}
			}
		} else if (y1 == y2) {
			if (x1 < x2) {
				for (int i = x1; i <= x2; i++) {
					ships[i][y1] = z;
				}
			} else if (x1 > x2) {
				for (int i = x1; i >= x2; i--) {
					ships[i][y1] = z;
				}
			}
		}
	}
	
	public boolean isPlaceAble(int x1, int y1, int x2, int y2, Ship z,
			Ship[][] ships) {
		if (x1 > 9 || x2 > 9 || y2 > 9 || y1 > 9 || x1 < 0 || x2 < 0 || y1 < 0
				|| y2 < 0) {
			// eliminate coordinates which are out of index range
			return false;
		} else if (x1 == x2 && y1 == y2) {
			return false;
		} else if (ships[x1][y1] == null && ships[x2][y2] == null) {
			// runs only if the coordinates are not reserved
			if (x1 == x2) {
				if (Math.abs(y1 - y2) != (z.getLength() - 1)) {
					// check the validity of length
					return false;
				} else if (y1 < y2) {
					if ((y1 - 1) >= 0) {
						if (ships[x1][y1 - 1] != null) {
							return false;
						}
					}
					if ((y2 + 1) <= 9) {
						if (ships[x1][y2 + 1] != null) {
							return false;
						}
					}
					//check for reserved indices
					for (int i = y1; i < y2; i++) {
						if (ships[x1][i + 1] != null) {
							return false;
						}
						if (((x1 + 1) <= 9)) {
							if (ships[x1 + 1][i] != null) {
								return false;
							}
						}
						if (((x1 - 1) >= 0)) {
							if (ships[x1 - 1][i] != null) {
								return false;
							}
						}

					}
					return true;
				} else if (y1 > y2) {
					if ((y1 + 1) <= 9) {
						if (ships[x1][y1 + 1] != null) {
							return false;
						}
					}
					if ((y2 - 1) >= 0) {
						if (ships[x1][y2 - 1] != null) {
							return false;
						}
					}
					// check for reserved indices
					for (int i = y1; i > y2; i--) {
						if (ships[x1][i - 1] != null) {
							return false;
						}
						if (((x1 + 1) <= 9)) {
							if (ships[x1 + 1][i] != null) {
								return false;
							}
						}
						if (((x1 - 1) >= 0)) {
							if (ships[x1 - 1][i] != null) {
								return false;
							}
						}

					}
					return true;
				} else {
					return true;
				}
			} else if (y1 == y2) {
				// nearly the same as the code for x1==x2
				if (Math.abs(x1 - x2) != (z.getLength() - 1)) {
					return false;
				} else if (x1 < x2) {
					if ((x1 - 1) >= 0) {
						if (ships[x1 - 1][y1] != null) {
							return false;
						}
					}
					if ((x2 + 1) <= 9) {
						if (ships[x2 + 1][y1] != null) {
							return false;
						}
					}
					for (int i = x1; i < x2; i++) {
						if (ships[i + 1][y1] != null) {
							return false;
						}
						if (((y1 + 1) <= 9)) {
							if (ships[i][y1 + 1] != null) {
								return false;
							}
						}
						if (((y1 - 1) >= 0)) {
							if (ships[i][y1 - 1] != null) {
								return false;
							}
						}

					}
					return true;
				} else if (x1 > x2) {
					if ((x1 + 1) <= 9) {
						if (ships[x1 + 1][y1] != null) {
							return false;
						}
					}
					if ((x2 - 1) >= 0) {
						if (ships[x2 - 1][y1] != null) {
							return false;
						}
					}
					for (int i = x1; i > x2; i--) {
						if (ships[i - 1][y1] != null) {
							return false;
						}
						if (((y1 + 1) <= 9)) {
							if (ships[i][y1 + 1] != null) {
								return false;
							}
						}
						if (((y1 - 1) >= 0)) {
							if (ships[i][y1 - 1] != null) {
								return false;
							}
						}

					}
					return true;
				} else {
					return true;
				}
			} else {
				return false;
			}

		} else {
			return false;
		}
	}
}
